package org.zn.etl.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Service;
import org.zn.etl.config.DevDataSourceInfo;
import org.zn.etl.datasource.DataSourceFactoryHolder;
import org.zn.etl.datasource.entity.DataSourceInfo;
import org.zn.etl.jdbc.ResultSetTools;
import org.zn.etl.misc.DataSourceInfo2ConfigMap;
import org.zn.etl.service.EtlDevService;

import com.google.common.collect.Lists;

@Service
public class EtlDevServiceImpl implements EtlDevService{

	@Autowired
	private DevDataSourceInfo devDataSourceInfo;
	
	@Autowired
	private DataSourceFactoryHolder factoryHolder ; 
	
	public void testSharingInsert() throws Exception {
		
		DataSourceInfo source= devDataSourceInfo.getDataSourceInfo("orcl1");
		DataSourceInfo target= devDataSourceInfo.getDataSourceInfo("shardingSphereDataSource");
		
		Map<String, String> sourceConfig=DataSourceInfo2ConfigMap.toMap(source);

		Map<String, String> targetConfig=DataSourceInfo2ConfigMap.toMap(target);
		
		DataSource sSource=factoryHolder.getFactory(source.getFactoryType()).getDataSource(source.getKeyName(), sourceConfig);
		
		DataSource tSource=factoryHolder.getFactory(target.getFactoryType()).getDataSource(target.getKeyName(), targetConfig);
		
		leasedInfo(sSource, tSource);
		
		//reginfo(sSource, tSource);
		
	}
	
	private void leasedInfo(DataSource sSource,DataSource tSource) throws SQLException{
		
		Connection conn= sSource.getConnection();
		
		ResultSet  rs2=conn.createStatement().executeQuery("select  l.* ,r.\"REGISTRATIONNO\"  from B_LEASEDINFO l JOIN  B_REGINFO r on l.\"REGISTRATIONID\"=r.\"REGISTRATIONID\"   where  ROWNUM<200");
		
		List<Map<String, Object>>  list=ResultSetTools.toMapReslt(rs2);
		
		conn.close();
		
		SimpleJdbcInsert insert=new SimpleJdbcInsert(tSource);
		
		insert.setTableName("t_leasedinfo");
		insert.setColumnNames(Lists.newArrayList(
				"leasedid", "registrationid", "registrationno", "leasedtype", "leasedcount", "leasedprice", "leasedcurrency", "orderby", "creatorid", "createdate", "updaterid", "updatedate"
				));

		
		Map<String,Object>[] params=new Map[list.size()];
		
		list.stream().map(it->{
			
			Map<String, Object> n=new HashMap<>();
			it.entrySet().stream().forEach(it2->{
				n.put(it2.getKey().toLowerCase(),it2.getValue());
			});
			return n;
		}).collect(Collectors.toList()).toArray(params);

		int[] up=insert.executeBatch(params);
	
		System.out.println(up);

	}
	
	
	private void reginfo(DataSource sSource,DataSource tSource) throws SQLException {
		
		Connection conn= sSource.getConnection();
		
		ResultSet  rs=conn.createStatement().executeQuery(
				"SELECT * from  B_REGINFO where REGISTRATIONID in (\r\n"
				+ "'fe037057-f055-4be5-ae93-b8455f74e803','fd87dcb3-4c6a-42e7-88da-09669d5eb518'\r\n"
				+ ")");
		
		List<Map<String, Object>>  list=ResultSetTools.toMapReslt(rs);
		
		conn.close();
		
		SimpleJdbcInsert insert=new SimpleJdbcInsert(tSource);
		
		insert.setTableName("t_reginfo");
		insert.setColumnNames(Lists.newArrayList("registrationid", "registrationno", "subregistrationno", "modifycode",
				"registrationinuredate", "registrationexpiredate", "realexpiredate", "regtimelimit", "extensionlimit",
				"personfillarchiveno", "authperson", "businesstype", "subbusinesstype", "mortgagetype", "registrationtype",
				"regentrytype", "objectiondesc", "terminatereason", "terminatereasondesc", "terminateflag", "canceldesc", "cancelflag",
				"objectioncancelflag", "filename", "identificationtype", "bustypename", "regedflag", "regeddate", "bustypeprovince",
				"bustypecity", "bustypecode", "bustype", "remark", "version_number", "bustypetext",
				"morregcno", "mortgagor", "creatorid", "createdate", "updaterid", "updatedate"));
		

		for (Map<String, Object> map : list) {
			
			Map<String, Object> n=new HashMap<>();
			map.entrySet().stream().forEach(it2->{
				n.put(it2.getKey().toLowerCase(),it2.getValue());
			});
			int up=insert.execute(n);	
			System.out.println(up);
		}
		
		
	}
	
}
